<?php

/**
 * View helper definition
 *
 * @category Forms
 * @package Front_Base_View
 * @subpackage Helper
 * @author Christian Soronellas <csoronellas@emagister.com>
 */

/**
 * Helper to generate an uneditable form textfield.
 *
 * @category Forms
 * @package Front_Base_View
 * @subpackage Helper
 * @author Christian Soronellas <csoronellas@emagister.com>
 */
class Front_Base_View_Helper_FormWysiwyg extends Zend_View_Helper_FormElement
{

    /**
     * Generates a 'text' element, that cannot be edited.
     *
     * @access public
     *
     * @param string|array $name If a string, the element name.  If an
     * array, all other parameters are ignored, and the array elements
     * are used in place of added parameters.
     *
     * @param mixed $value The element value.
     *
     * @param array $attribs Attributes for the element tag.
     *
     * @return string The element XHTML.
     */
    public function formWysiwyg($name, $value = null, $attribs = null)
    {
        $ckEditorPath = BASE_PATH . DS . 'public/libs/ckeditor';
        require_once ($ckEditorPath . '/ckeditor.php');

        if (is_null($name) && is_null($value) && is_null($attribs)) {
            return $this;
        }
        $info = $this->_getInfo($name, $value, $attribs);
        extract($info); // name, value, attribs, options, listsep, disable

        $editor = new CKEditor();
        // let returns, but does not display in the browser
        $editor->returnOutput = true;

        // sets the base path to the visual editor
        $editor->basePath = '/libs/ckeditor/';

        //width of the editor
        $editor->config['width'] = 'auto';

        $editor->config['filebrowserBrowseUrl'] = '/libs/kcfinder/browse.php?type=files&currentFolder=/uploads/files/';
        $editor->config['filebrowserImageBrowseUrl'] = '/libs/kcfinder/browse.php?type=images&currentFolder=/uploads/images/';
        $editor->config['filebrowserFlashBrowseUrl'] = '/libs/kcfinder/browse.php?type=flash&currentFolder=/uploads/flash/';
        $editor->config['filebrowserUploadUrl'] = '/libs/kcfinder/upload.php?type=files&currentFolder='.BASE_PATH.'/data/uploads/files/';
        $editor->config['filebrowserImageUploadUrl'] = '/libs/kcfinder/upload.php?type=images&currentFolder='.BASE_PATH.'/data/uploads/images/';
        $editor->config['filebrowserFlashUploadUrl'] = '/libs/kcfinder/upload.php?type=flash&currentFolder='.BASE_PATH.'/data/uploads/images/';
        $editor->config['image_previewText'] = '';

        // Configuration that will only be used by the second editor.
        $config = array();
        if(isset($attribs['editorToolbar'])) {
            $toolbar = $attribs['editorToolbar'];
            switch ($toolbar) {
                case 'custom':
                    if(isset($attribs['editorCustomConfig']) && is_array($attribs['editorCustomConfig'])) {
                        $config['toolbar'] = $attribs['editorCustomConfig'];
                    }
                    break;
                case 'basic':
                    $config['toolbar'] = array(
                        array('Source', '-', 'Bold', 'Italic', 'Underline', 'Strike'),
                        array('Image', 'Link', 'Unlink', 'Anchor')
                    );
                    break;
                default:
                    break;
            }
        }
        if(isset($attribs['editorHeight'])) {
            $config['height'] = $attribs['editorHeight'];
        }
        if(isset($attribs['editorWidth'])) {
            $config['width'] = $attribs['editorWidth'];
        }

        $_SESSION['KCFINDER'] = array();
        $_SESSION['KCFINDER']['disabled'] = false;
        $_SESSION['KCFINDER']['uploadURL'] = "/uploads";
        $_SESSION['KCFINDER']['uploadDir'] = "";

        // $value contains the default value
        return $editor->editor($name, $value, $config);
    }

}
